import React, { Component } from 'react'

import  userModel from  "../services/userinfo/Service"

interface  IState{
    username:string,
    userpwd:string,
}
export default class Home extends Component<any,IState> {
    constructor(props:any){
        super(props);
        this.state = {
            username:"",
            userpwd:""
        }
    }
    login =async ()=>{
        let  {data:list} = await  userModel.query({username:this.state.username,userpwd:this.state.userpwd});
        if(list.length){
            window.localStorage.setItem("userinfo",JSON.stringify(list[0]));
            this.props.history.push("/main");
        }else{
            alert("用户名和密码不匹配");    
        } 
    }

  render() {
    return (
      <div>
          <div>用户名:<input type="text"  value={this.state.username} onChange={(e)=>{
              this.setState({
                  username:e.target.value
              })

          }}/></div>
            <div>密码: <input type="text"  value={this.state.userpwd} onChange={(e)=>{
              this.setState({
                  userpwd:e.target.value
              })

          }}/></div>
          <button onClick={()=>{
              this.login();
          }}>登录</button>
      </div>
    )
  }
}
